package com.dragon.app.controller.auth;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.app.config.annotation.Login;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.avttendance.IDailyLogsService;
import com.dragon.repository.domain.dto.DailyLogsDTO;
import com.dragon.repository.domain.vo.DailyLogsVO;
import com.dragon.repository.entity.DailyLogs;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 工作汇报
 * @author Charlotte
 */
@RestController
@RequestMapping("dailyLogs")
@Api(tags = "工作汇报")
public class DailyLogsController {

    @Autowired
    IDailyLogsService service;

    @GetMapping("list")
    @ApiOperation("日报列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminIds", value = "用户ids", required = false, type = "int"),
            @ApiImplicitParam(name = "time", value = "日期时间", required = false, type = "string"),
            @ApiImplicitParam(name = "type", value = "类型", required = false, type = "int")
    })
    public ResponseData<Map<String, List<DailyLogs>>>  dailyList( @RequestParam(required = false) Integer adminIds,
                                                                  @RequestParam(required = false) String time,
                                                                  @RequestParam(required = false) Integer type){
        Map<String, List<DailyLogs>> dailyLogs = service.dailyList(adminIds,time,type);
        return ResponseData.success(dailyLogs);
    }

    @PostMapping("add")
    @ApiOperation("添加")
    public ResponseData addDaily(@RequestBody @Validated(Insert.class)DailyLogsVO vo){
        service.addDaily(DailyLogsVO.convert(vo),null);
        return ResponseData.success();
    }

    @PostMapping("update")
    @ApiOperation("修改")
    public ResponseData updateDaily(@RequestBody @Validated(Update.class)DailyLogsVO vo){
        service.updateDaily(DailyLogsVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation("删除")
    public ResponseData deleteDaily(@ApiParam(name = "ids",value = "ids",required = true, type = "string")
                                    @RequestParam String ids){
        service.deleteDaily(ids);
        return ResponseData.success();
    }


    @PostMapping("showDailyLogOne")
    @ApiOperation("查看当天日报")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminId",value = "用户id", required = true, type = "int"),
            @ApiImplicitParam(name = "nowTime",value = "时间", required = true, type = "string")
    })
    public ResponseData<DailyLogsDTO> showDailyLogOne(@RequestParam Integer adminId, @RequestParam String nowTime){
        DailyLogs dailyLogs = service.showDailyLogOne(adminId, nowTime);
        return ResponseData.success(DailyLogsDTO.convert(dailyLogs));
    }

    @PostMapping("showCertainOne")
    @ApiOperation("查看某个详情")
    public ResponseData<DailyLogsDTO> showCertainOne(@ApiParam(name = "id",value = "id", required = true,type = "int")
                                                     @RequestParam Integer id){
        DailyLogs dailyLogs = service.showCertainOne(id);
        return ResponseData.success(DailyLogsDTO.convert(dailyLogs));
    }

    @GetMapping("normalList")
    @ApiOperation("分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "条数", required = true, type=  "int"),
            @ApiImplicitParam(name = "search", value = "搜索参数（用户id）", required = false, type = "int"),
            @ApiImplicitParam(name = "time", value = "时间", required = false, type = "string"),
            @ApiImplicitParam(name = "type", value = "类型", required = false, type = "int")
    })
    public ResponseData<List<DailyLogsDTO>> normalList(@RequestParam Integer page,
                                                       @RequestParam Integer limit,
                                                       @RequestParam(required = false) Integer search,
                                                       @RequestParam(required = false) String time,
                                                       @RequestParam(required = false) Integer type){
        Page<DailyLogs> dailyLogsPage =  service.normalList(page, limit, search, time, type);
        return ResponseData.success(DailyLogsDTO.convertList(dailyLogsPage.getRecords()),dailyLogsPage.getTotal());
    }

    @GetMapping("submitList")
    @ApiOperation("发送列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "条数", required = true, type=  "int"),
            @ApiImplicitParam(name = "type", value = "类型", required = false, type = "int"),
            @ApiImplicitParam(name = "time", value = "时间", required = false, type = "string")
    })
    public ResponseData<List<DailyLogsDTO>> receptionList(
            @RequestParam Integer page,
            @RequestParam Integer limit,
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) String time,
            @Login @ApiParam(hidden = true) Integer adminId){
        Page<DailyLogs> dailyLogsPage =  service.submitList(page, limit, type, time ,adminId);
        return ResponseData.success(DailyLogsDTO.convertList(dailyLogsPage.getRecords()),dailyLogsPage.getTotal());
    }

    @GetMapping("receptionList")
    @ApiOperation("接收列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "条数", required = true, type=  "int"),
            @ApiImplicitParam(name = "type", value = "类型", required = false, type = "int"),
            @ApiImplicitParam(name = "time", value = "时间", required = false, type = "string"),
            @ApiImplicitParam(name = "search", value = "搜索参数（用户id）", required = false, type = "int")
    })
    public ResponseData<List<DailyLogsDTO>> receptionList(
            @RequestParam Integer page,
            @RequestParam Integer limit,
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) String time,
            @Login @ApiParam(hidden = true) Integer adminId,
            @RequestParam(required = false) Integer search){
        Page<DailyLogs> dailyLogsPage =  service.receptionList(page, limit, type, time, adminId, search);
        return ResponseData.success(DailyLogsDTO.convertList(dailyLogsPage.getRecords()),dailyLogsPage.getTotal());
    }

    @GetMapping("statistics")
    @ApiOperation(value = "日报统计")
    public ResponseData<Map<String,Object>> dailyLogNum(
            @ApiParam(name = "time", value = "时间", required = true, type = "string")
            @RequestParam String time
    ){
        return ResponseData.success(service.statistics(time));
    }
}
